(Probe FRONT LEFT TOP with Bisection. Position probe up to 15mm inland and above corner)

; Set user-defined variables

%PROBE_TIP_DIA = 2.0		; Diameter in mm
%PROBE_INERTIA_Z = 0.00		; Machine overshoot Z due to inertia
%PROBE_INERTIA_XY = 0.00	; Machine overshoot XY due to inertia

%PROBE_BLOCK_X = 0		; Probe/puck thickness, X
%PROBE_BLOCK_Y = 0		; Probe/puck thickness, Y
%PROBE_BLOCK_Z = 0		; Probe/puck thickness, Z

%PROBE_PROBE_RANGE = 15.0	; Range that G38.x will use
%PROBE_CLOSE_RANGE = 1.0	; Distance to retreat before slow probe
%PROBE_PROBE_SLOW = 10.0	; Slow probe rate for G38.x
%PROBE_PROBE_FAST = 150.0	; Fast probe rate for G38.x
%PROBE_DROP_BY = 6.0		; Distance Z drops before probing (typ double WITHDRAW)
%PROBE_WITHDRAW = 3.0		; Distance axes pull back after probing

; Wait until the planner queue is empty

%wait

; *** FRONT LEFT 'TO and AWAY' PROBE XYZ ***

G21		; Make sure we’re in mm
M5		; Just to be sure, ensure spindle is stopped
G91 		; Incremental mode

; *** Z Probe *** Downwards is -ve

G38.2 Z[-PROBE_PROBE_RANGE] F[PROBE_PROBE_FAST]	; First probe quickly to find Z
G0 Z[PROBE_CLOSE_RANGE]	; Withdraw now Z crudely located, raise Z a little

G38.2 Z[-PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Now probe TO slowly to reduce inertia
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%Z1 = posz		; Capture position 1 after probing
G38.4 Z[PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Slow AWAY probe
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%Z2 = posz		; Capture position 2 after probing

; Halve Z1 vs Z2 position difference (Z2 will be smaller, less -ve than Z1)
%ZDIFF = [Z1-Z2]/2	; eg: -25.0 - -24.7 = -0.3 /2 = -0.15

G10 L20 P1 Z[ZDIFF +PROBE_BLOCK_Z -PROBE_INERTIA_Z]	;Set Z0 with compensations
G0 Z[PROBE_WITHDRAW]	; Pull up Z to clear puck/workpiece

; *** X Probe *** Rightwards is +ve

G0 X[-PROBE_PROBE_RANGE]	; Move left
G0 Z[-PROBE_DROP_BY]		; Drop Z below top of puck/workpiece
G38.2 X[PROBE_PROBE_RANGE] F[PROBE_PROBE_FAST]	; Probe TO X to the right quickly
G0 X[-PROBE_CLOSE_RANGE]	; Withdraw a little

G38.2 X[PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Now probe TO slowly to reduce inertia
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%X1=posx
G38.4 X[-PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Slow AWAY probe
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%X2=posx

; Halve X1 vs X2 position difference eg: X1 -160 and X2 -160.3 (probe direction sensitive)
%XDIFF = [X2-X1]/2

G10 L20 P1 X[XDIFF -PROBE_TIP_DIA/2.0 +PROBE_BLOCK_X -PROBE_INERTIA_XY]	; Set X0 with compensations
G0 X[-PROBE_WITHDRAW]	; Move left by retract distance to clear block
G0 Z[PROBE_DROP_BY]	; Raise Z to avoid conflict when moving

; *** Y Probe *** Backwards is +ve

G0 Y[-PROBE_PROBE_RANGE]	; Move forward towards front of machine
G0 X[PROBE_PROBE_RANGE]		; Move right
G0 Z[-PROBE_DROP_BY]		; Drop Z below top of puck/workpiece
G38.2 Y[PROBE_PROBE_RANGE] F[PROBE_PROBE_FAST]	; Probe TO Y rearwards quickly
G0 Y[-PROBE_CLOSE_RANGE]	; Withdraw a little forwards

G38.2 Y[PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Probe TO Y slowly
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%Y1=posy
G38.4 Y-[PROBE_PROBE_RANGE] F[PROBE_PROBE_SLOW]	; Probe AWAY Y to the front slowly
G4 P0.1			; Pause to let Grbl/EEPROM catch up
%Y2=posy

; Halve Y1 vs Y2 position difference eg: Y1=-200.3 and Y2=-200.0
%YDIFF = [Y1-Y2]/2

G10 L20 P1 Y[-YDIFF -PROBE_TIP_DIA/2.0 -PROBE_BLOCK_Y +PROBE_INERTIA_XY]	; Set Y0 with compensations
G4 P0.1			; Pause to let Grbl/EEPROM catch up
G0 Y[-PROBE_WITHDRAW]	; Move forward to clear puck/workpiece
G0 Z[PROBE_DROP_BY]	; Raise Z to avoid conflict when moving

; *** Wrap-up ***

G90		; Back into Absolute mode
G0 X0Y0		; Go to this new X0Y0

; *** All Done ***